let selt = document.querySelector(".sel");
let op = document.querySelector(".op");
let tbody = document.querySelector("tbody");
selt.innerHTML = `<option value="">${new Date().toLocaleDateString()}</option>`;

selt.addEventListener("click", () => {
    op.style.display = "block";
    op.innerHTML = "";
    ajax("http://newshopapi.0melon0.cn/api/f_user/chooselist", {
        "token": localStorage.getItem("token")
    }, "GET").then(res => {
        let times = res.result.list;
        times.forEach((item, idx) => {
            // console.log(item);
            op.innerHTML += `<div>${item.day_time}</div>`;
        });
        let list = document.querySelectorAll(".op>div");
        list.forEach((item, idx) => {
            item.addEventListener("click", () => {
                selt.innerHTML = item.innerText;
                op.style.display = "none";
                ajax("http://newshopapi.0melon0.cn/api/f_user/chooseday", {
                    "token": localStorage.getItem("token"),
                    day_time: item.innerText
                }, "GET").then(res => {
                    // console.log(res);
                    let t = res.result.list;
                    tbody.innerHTML = "";
                    t.forEach((item, idx) => {
                        let time = Date.now();
                        console.log(item.show_name);
                        let d1 = item.day_time + " " + item.show_name.split("-")[0];
                        let d2 = item.day_time + " " + item.show_name.split("-")[1];
                        let d3 = item.day_time + "-" + item.show_name.split("-")[0];
                        let times = d3;
                        localStorage.setItem("times", times);
                        let datatime1 = new Date(`${d1}`).getTime();
                        let datatime2 = new Date(`${d2}`).getTime();
                        // 状态栏
                        function zt() {
                            if ((datatime1 < time && datatime2 > time) || (datatime1 > time && datatime2 > time)) {
                                return "可预约";

                            } else {
                                return "不可预约";
                            }
                        }
                        tbody.innerHTML += `<tr>
                        
                        <th>${item.show_name}</th>
                        <th><a href="">${zt()}</a></th>
                        
                      </tr>`;

                        //tr点击
                        let lists = tbody.children;
                        let tr = document.querySelectorAll("tbody>tr");
                        tr.forEach((item, idx) => {
                            item.addEventListener("click", () => {
                                // console.log(res.result.list[0].day_time);
                                let text = tbody.children[idx].children[1].children[0];
                                let zts = text.innerText;
                                let days=tbody.children[idx].children[0].innerText;
                                ajax("http://newshopapi.0melon0.cn/api/f_user/bookday", {
                                    "token": localStorage.getItem("token"),
                                    "day_time": res.result.list[0].day_time,
                                    "show_id": res.result.list[idx].show_id
                                }, "POST").then(res => {
                                    console.log(res);
                                    let id = res.result.id;
                                    let name = res.result.name
                                    // console.log(name);
                                    localStorage.setItem("id", id);
                                    localStorage.setItem("name", name);
                                }).catch(err => {
                                    console.log(err);
                                });
                                if (zts == "可预约") {
                                    localStorage.setItem("days", days);

                                    text.href = "./erwm.html";
                                    // text.href = "#";

                                } else {
                                    text.href = "#";
                                    layer.msg('此时间不可预约');
                                }
                            })
                        })
                    });
                    return ajax("http://newshopapi.0melon0.cn/api/f_user/bookday", {
                        "token": localStorage.getItem("token"),
                        day_time: item.innerText,
                        show_id: "1"
                    }, "POST")
                }).catch(err => {
                    console.log(err);
                });
            })
        })
    }).catch(err => {
        console.log(err);
    });
});

